package com.study.homework.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
	
	public static void main(String[] args) {
//		ExecutorService threadPool = Executors.newFixedThreadPool(3);//固定大小线程池
		ExecutorService threadPool = Executors.newCachedThreadPool();//缓存线程池,大小可自动扩展
//		ExecutorService threadPool = Executors.newSingleThreadExecutor();//创建一个线程的线程池,保证池中肯定有一个线程存活
		for (int i = 0; i < 10; i++) {
			final int task =i;
			threadPool.execute(new Runnable() {
				
				@Override
				public void run() {
					for (int j = 0; j < 10; j++) {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName()+"is looping of"+j+"for task"+task);
					}
					
				}
			});
		}
		System.out.println("all of 10 tasks have committed!");
		
		Executors.newScheduledThreadPool(3).schedule(new Runnable(){

			@Override
			public void run() {
				System.out.println("boombing");
				
			}}, 10, TimeUnit.SECONDS);
	}
}
